package com.wdg.common.page;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Pagination<T> extends BasePage implements Serializable, Paginable {
    private List<T> list;

    public Pagination() {
    }

    public Pagination(int pageNum, int pageSize, int totalCount) {
        super(pageNum, pageSize, totalCount);
    }

    public Pagination(int pageNum, int pageSize, int totalCount, List<T> list) {
        super(pageNum, pageSize, totalCount);
        this.list = list;
    }

    public int getFirstResult() {
        return (this.pageNum - 1) * this.pageSize;
    }

    public List<T> getList() {
        return this.list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public <S> Pagination<S> of(List<S> list) {
        return new Pagination(this.pageNum, this.pageSize, this.totalCount, list);
    }

    public <S> Pagination<S> map(Function<T, S> mapFunction) {
        if (this.list == null) {
            return new Pagination(this.pageNum, this.pageSize, this.totalCount, Collections.EMPTY_LIST);
        } else {
            List<S> reList = (List)this.list.stream().map(mapFunction).collect(Collectors.toList());
            return new Pagination(this.pageNum, this.pageSize, this.totalCount, reList);
        }
    }
}